package com.ytjj.qmyx.admin.service.impl;

import com.gexin.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.dao.BankerDao;
import com.ytjj.qmyx.admin.dao.BankerInfoDao;
import com.ytjj.qmyx.admin.dao.BankerMessageDao;
import com.ytjj.qmyx.admin.dao.BankerSiteDao;
import com.ytjj.qmyx.admin.model.request.BankerSiteRequest;
import com.ytjj.qmyx.admin.model.response.BankerResponse;
import com.ytjj.qmyx.admin.model.response.BankerSiteResponse;
import com.ytjj.qmyx.admin.service.BankerSiteService;
import com.ytjj.qmyx.admin.service.MemberService;
import com.ytjj.qmyx.mall.mapper.BankerSiteMapper;
import com.ytjj.qmyx.mall.model.BankerInfo;
import com.ytjj.qmyx.mall.model.BankerMessage;
import com.ytjj.qmyx.mall.model.BankerSite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 * 
 * @Version: 1.0
 */
@Service
public class BankerSiteServiceImpl implements BankerSiteService {

    @Autowired
    private BankerSiteDao bankerSiteDao;

    @Autowired
    private BankerSiteMapper bankerSiteMapper;

    @Autowired
    private BankerDao bankerDao;

    @Autowired
    private BankerInfoDao bankerInfoDao;

    @Autowired
    private BankerMessageDao bankerMessageDao;

    @Autowired
    private MemberService memberService;

    @Resource
    private SupplyApiConfig supplyApiConfig;

    @Override
    public CommonPage<BankerSiteResponse> selectList(BankerSiteRequest bankerSiteRequest) {
        PageHelper.startPage(bankerSiteRequest.getPageNum(), bankerSiteRequest.getPageSize());
//        String curUserEmail = UserUtil.getPrincipal();
//        int adminId =adminMapper.adminId(curUserEmail);
//        bankerSiteRequest.setAdminId(adminId);
        List<BankerSiteResponse> pageList = bankerSiteDao.selectList(bankerSiteRequest);
        if(!CollectionUtils.isEmpty(pageList)) {
            for(BankerSiteResponse bsr : pageList) {
                if (null == bsr.getBrandType()) {
                    bsr.setBrandType(bsr.getBankerType());
                }
                if(bsr.getBankerId() != null) {
                    List<BankerResponse> specialBrand = bankerDao.getSpecialBrand(bsr.getBankerId());
                    bsr.setBankerResponseList(specialBrand);
                }
            }
        }
        return CommonPage.restPage(pageList);
    }

    @Override
    public String newSelectList(BankerSiteRequest bankerSiteRequest) {
        String json = JSON.toJSONString(bankerSiteRequest);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/siteList", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateStatus(BankerSiteRequest bankerSiteRequest) {
        BankerSite bankerSite = bankerSiteDao.getBankerSite(bankerSiteRequest.getId());
        Integer count = 0;
        if(bankerSite.getStatus() == 0) {
            // 上架 前提：相应的供应商资质通过审核
            // 获取供应商信息
            BankerInfo bankerInfo = bankerInfoDao.getBankerInfoStatus(bankerSite.getBankerId());
            if(bankerInfo != null) {
                if(bankerInfo.getStatus() == 1) {
                    throw new ApiException("供应商资质审核中，暂时无法上架该品牌！");
                }
                if(bankerInfo.getStatus() == 3){
                    throw new ApiException("供应商资质审核失败，暂时无法上架该品牌！");
                }
                count = bankerSiteDao.updateStatus(bankerSite.getId(), 1, memberService.getAdmin().getName());
                if(count > 0) {
                    BankerMessage message = new BankerMessage();
                    message.setBankerId(bankerSite.getBankerId());
                    message.setBankerName(bankerSite.getBankerName());
                    message.setRemarks("您的官网名为："+bankerSite.getTitle()+" 已审核通过！");
                    message.setStatus(0);
                    message.setType(7);
                    message.setCreateTime(new Date());
                    bankerMessageDao.insertMessage(message);
                }
            } else {
                throw new ApiException("未找到该品牌对应的供应商资质信息，暂时无法进行操作！");
            }
        } else {
            count = bankerSiteDao.updateStatus(bankerSite.getId(), 0, memberService.getAdmin().getName());
            if(count > 0) {
                BankerMessage message = new BankerMessage();
                message.setBankerId(bankerSite.getBankerId());
                message.setBankerName(bankerSite.getBankerName());
                message.setRemarks("您的官网名为：" + bankerSite.getTitle() + " 审核失败，原因：(" + bankerSiteRequest.getRemarks() + ")");
                message.setStatus(0);
                message.setType(8);
                message.setCreateTime(new Date());
                bankerMessageDao.insertMessage(message);
            }
        }
        return count;
    }

    @Override
    public Integer updateBankerSite(BankerSite bankerSite) {
        bankerSite.setUpdateTime(new Date());
        bankerSite.setAdminName(memberService.getAdmin().getName());
        return bankerSiteMapper.updateByPrimaryKeySelective(bankerSite);
    }
}
